package com.zmcc.clients;

import com.google.gson.Gson;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.zmcc.entity.*;
import com.zmcc.entity.common.CommonRequest;
import com.zmcc.entity.common.CommonResponse;
import com.zmcc.sender.DefaultSyncClient;
import com.zmcc.sender.ISyncClient;
import com.zmcc.service.ICallTipsRequest;
import com.zmcc.service.impl.CallTipsCommercial;
import com.zmcc.service.impl.EnMmsCommercial;
import com.zmcc.utils.WrapperUtils;

import java.io.File;
import java.util.List;

public class EnMmsClient {
    private final ISyncClient syncClient;

    private final ICallTipsRequest requestWrapper;

    private final String appkey;

    private final String appsecret;

    private final String mode;

    public EnMmsClient(Builder builder) {
        this.appkey = builder.appkey;
        this.appsecret = builder.appsecret;
        this.mode = builder.mode;
        this.syncClient = DefaultSyncClient.getInstance();
        this.requestWrapper = EnMmsCommercial.getInstance();
    }

    public static class Builder {
        private String appkey;
        private String appsecret;
        private String mode;

        public Builder() {

        }

        public Builder appkey(String appkey) {
            this.appkey = appkey;
            return this;
        }

        public Builder appsecret(String appsecret) {
            this.appsecret = appsecret;
            return this;
        }

        public Builder mode(String mode) {
            this.mode = mode;
            return this;
        }

        public EnMmsClient build() {
            return new EnMmsClient(this);
        }
    }

    public CallTipsUpdateResult updateCropUserInfo(CallTipsCorpUserInfoTemplate template) {
        CommonRequest request = requestWrapper.getUpdateCorpUserInfoRequest(template);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response = this.syncClient.fetch(request);
        return new Gson().fromJson(response.getData(), CallTipsUpdateResult.class);
    }

    public String addTemplate(String subtitle, String templateContent) {
        CommonRequest request = requestWrapper.getAddTemplateRequest(subtitle, templateContent);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response = this.syncClient.fetch(request);
        return new JsonParser().parse(response.getData()).getAsJsonObject().get("templateID").getAsString();
    }

    public void delTemplate(String templateID) {
        CommonRequest request = requestWrapper.getDelTemplateRequest(templateID);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response = this.syncClient.fetch(request);
    }

    public void updateTemplate(String subtitle, String templateContent) {
        CommonRequest request = requestWrapper.getUpdateTemplateRequest(subtitle, templateContent);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response = this.syncClient.fetch(request);
    }

    public CallTipsUpdateResult addSetting(CallTipsSettingTemplate template) {
        CommonRequest request = requestWrapper.getAddSettingRequest(template);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response = this.syncClient.fetch(request);
        return new Gson().fromJson(response.getData(), CallTipsUpdateResult.class);
    }

    public List<CallTipsSettingSnapResult> getSettingSnap(String beginTime, String endTime, String[] phoneNumberList) {
        CommonRequest request = requestWrapper.getQuerySettingRequest(beginTime, endTime, phoneNumberList);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response = this.syncClient.fetch(request);
        return new Gson().fromJson(new JsonParser().parse(response.getData()).getAsJsonObject().get("settingInfoList").getAsString()
                , new TypeToken<List<CallTipsSettingSnapResult>>(){}.getType());
    }

    public CallTipsSettingInfoListResult getSettingDetail(String ruleId) {
        CommonRequest request = requestWrapper.getSettingDetailRequest(ruleId);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response = this.syncClient.fetch(request);
        return new Gson().fromJson(response.getData(), CallTipsSettingInfoListResult.class);
    }

    public void delSetting(String ruleId, String[] phoneNumberList) {
        CommonRequest request = requestWrapper.getDelSettingRequest(ruleId, phoneNumberList);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response = this.syncClient.fetch(request);
    }

    public String uploadFile(File file) {
        CommonRequest request = requestWrapper.getUploadRequest();
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(this.appkey, this.appsecret)));
        CommonResponse response = this.syncClient.uploadFile(request, file);
        return new JsonParser().parse(response.getData()).getAsJsonObject().get("fileId").getAsString();
    }

    public String releaseMaterial(EnMmsReleaseTemplate template) {
        CommonRequest request = requestWrapper.getReleaseRequest(template);
        request.setHeaderParams(WrapperUtils.authHeaderToMap(WrapperUtils.authHeader(appkey, appsecret)));
        CommonResponse response = this.syncClient.fetch(request);
        return new JsonParser().parse(response.getData()).getAsJsonObject().get("cyId").getAsString();
    }
}
